import { formItemValidateStates } from 'element-plus'
import { defineStore } from 'pinia'

export const usePaperStore = defineStore('paperSetting', {
    state: () => ({
        paperSetting: {
            paperType: "考试",
            paperSize: "A4",
            mainTitle: '',
            subtitle: { isDisplay: false, content: '' },
            isBindLineDisplay: false,
            secretMark: { isDisplay: false, content: '' },
            paperInfo: { isDisplay: false, content: '' },
            candidateInput: { isDisplay: false, content: '' },
            paperScore: false,
            paperAttention: { isDisplay: false, content: '' },
            subsectionAndNote: { 
                chapters: [],
                isDisplay: false
            },
            bigTopicAndNote: false,
            isBigTopicScoreDisplay: false,
            questionTypeOrder: null
        },
        paperStructure: undefined, // 试卷结构,
        paperTypeConfigs: {
            work: [
                { label: '主标题', key: 'mainTitle', checked: true, disabled: true },
                { label: '副标题', key: 'subtitle', checked: false, disabled: false },
                { label: '装订线', key: 'isBindLineDisplay', checked: false, disabled: false },
                { label: '保密标记', key: 'secretMark', checked: false, disabled: false },
                { label: '试卷信息栏', key: 'paperInfo', checked: false, disabled: false },
                { label: '考生输入栏', key: 'candidateInput', checked: false, disabled: false },
                { label: '誉分栏', key: 'paperScore', checked: false, disabled: false },
                { label: '注意事项', key: 'paperAttention', checked: false, disabled: false },
                { label: '分卷及注释', key: 'subsectionAndNote', checked: false, disabled: false },
                { label: '大题及注释', key: 'bigTopicAndNote', checked: false, disabled: false },
                { label: '大题及评分', key: 'isBigTopicScoreDisplay', checked: false, disabled: false }
            ],
            test: [
                { label: '主标题', key: 'mainTitle', checked: true, disabled: true },
                { label: '副标题', key: 'subtitle', checked: false, disabled: false },
                { label: '装订线', key: 'isBindLineDisplay', checked: false, disabled: false },
                { label: '保密标记', key: 'secretMark', checked: false, disabled: false },
                { label: '试卷信息栏', key: 'paperInfo', checked: false, disabled: false },
                { label: '考生输入栏', key: 'candidateInput', checked: false, disabled: false },
                { label: '誉分栏', key: 'paperScore', checked: false, disabled: false },
                { label: '注意事项', key: 'paperAttention', checked: false, disabled: false },
                { label: '分卷及注释', key: 'subsectionAndNote', checked: false, disabled: false },
                { label: '大题及注释', key: 'bigTopicAndNote', checked: false, disabled: false },
                { label: '大题及评分', key: 'isBigTopicScoreDisplay', checked: false, disabled: false }
            ], 
            exam: [
                { label: '主标题', key: 'mainTitle', checked: true, disabled: true },
                { label: '副标题', key: 'subtitle', checked: false, disabled: false },
                { label: '装订线', key: 'isBindLineDisplay', checked: false, disabled: false },
                { label: '保密标记', key: 'secretMark', checked: false, disabled: false },
                { label: '试卷信息栏', key: 'paperInfo', checked: false, disabled: false },
                { label: '考生输入栏', key: 'candidateInput', checked: false, disabled: false },
                { label: '誉分栏', key: 'paperScore', checked: false, disabled: false },
                { label: '注意事项', key: 'paperAttention', checked: false, disabled: false },
                { label: '分卷及注释', key: 'subsectionAndNote', checked: false, disabled: false },
                { label: '大题及注释', key: 'bigTopicAndNote', checked: false, disabled: false },
                { label: '大题及评分', key: 'isBigTopicScoreDisplay', checked: false, disabled: false }
            ] 
        },
    }),
    getters: {
    },
    actions: {
        // 更新配置项选中状态
        updateConfigCheck(val, key, checked) {
            const config = this.paperTypeConfigs[val].find(item => item.key === key)
            if (config && !config.disabled) {
                config.checked = checked
                // 更新 paperSetting 中对应项的 isDisplay
                if (key === 'isBindLineDisplay' || key === 'isBigTopicScoreDisplay' || key === 'paperScore' || key === 'bigTopicAndNote') {
                    this.paperSetting[key] = checked
                } else {
                    this.paperSetting[key].isDisplay = checked
                }
                // 保存到 localStorage
                // localStorage.setItem(`paperSetting.${templateKey}`, JSON.stringify(this.templateConfigs[templateKey]))
                this.paperSetting.paperTypeConfigs = {
                    // ...this.paperSetting.paperTypeConfigs,
                    [val]: this.paperTypeConfigs[val]
                }
            }
        },

        

        initPaperSetting(paperData, paperTypeOptions) {
            if (paperData?.paperSetting) {
                // 保留初始结构并覆盖实际数据
                this.paperSetting = {
                    ...this.paperSetting,
                    ...paperData.paperSetting
                }

                // 获取当前试卷类型的配置项l
                const paperType = paperData.paperSetting.paperType
                const curPaperTypeOptions = paperTypeOptions.find(option => option.label === paperType)


                const updateConfigs = [
                    { label: '主标题', key: 'mainTitle', checked: true, disabled: true },
                    { label: '副标题', key: 'subtitle', checked: paperData.paperSetting.subtitle.isDisplay, disabled: false },
                    { label: '装订线', key: 'isBindLineDisplay', checked: paperData.paperSetting.isBindLineDisplay, disabled: false },
                    { label: '保密标记', key: 'secretMark', checked: paperData.paperSetting.secretMark.isDisplay, disabled: false },
                    { label: '试卷信息栏', key: 'paperInfo', checked: paperData.paperSetting.paperInfo.isDisplay, disabled: false },
                    { label: '考生输入栏', key: 'candidateInput', checked: paperData.paperSetting.candidateInput.isDisplay, disabled: false },
                    { label: '誉分栏', key: 'paperScore', checked: paperData.paperSetting.paperScore, disabled: false },
                    { label: '注意事项', key: 'paperAttention', checked: paperData.paperSetting.paperAttention.isDisplay, disabled: false },
                    { label: '分卷及注释', key: 'subsectionAndNote', checked: paperData.paperSetting.subsectionAndNote.isDisplay, disabled: false },
                    { label: '大题及注释', key: 'bigTopicAndNote', checked: paperData.paperSetting.bigTopicAndNote, disabled: false },
                    { label: '大题及评分', key: 'isBigTopicScoreDisplay', checked: paperData.paperSetting.isBigTopicScoreDisplay, disabled: false }
                ]
                // 把更新后的配置项应用到paperTypeConfigs
                this.paperTypeConfigs[curPaperTypeOptions?.value] = updateConfigs

                // 单独存储paperStructure
                this.paperStructure = paperData.paperStructure || undefined
            }
        }
    },
    persist: true
    // persist: [
    //     {
    //         pick: ['paperSetting'],
    //         storage: localStorage

    //     }
    // ],
})